﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

/* Copyright (c) 2014
 * author: Ricky Kratochwil
 * @ date: 2014-06-27
 * @ summary: World selection or creation
*/

namespace GameOfCoins
{
	public partial class WorldEditorSetup : Form
	{
		CWorldEditor m_oWorldEditor;		//Use for initialization calls

		public WorldEditorSetup(CWorldEditor oWorldEditor)
		{
			InitializeComponent();

			m_oWorldEditor = oWorldEditor;

			LoadAvailableWorlds();
		}

//////////////////////////////////////////////////////////////////////////////////////////////////////
////////// UTILITIES

		//Load saved worlds and display for selection
		//@ KR / 2014.06.27
		private void LoadAvailableWorlds()
		{
			string[] strWorldFiles = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\Worlds", "*.gcw");
			int iStart, iLenght;

			foreach(string strFile in strWorldFiles)
			{
				iStart	= strFile.LastIndexOf('\\')+1;
				iLenght	= strFile.LastIndexOf('.') - iStart;

				lbWorldsAvail.Items.Add(strFile.Substring(iStart, iLenght));
			}
		}

//////////////////////////////////////////////////////////////////////////////////////////////////////
////////// COMMOM CTRLS

		//Click on load
		//@ KR / 2014.06.27
		private void btnLoad_Click(object sender, EventArgs e)
		{
			//Check if all selections are done
			if(lbWorldsAvail.SelectedIndex == -1)
			{
				MessageBox.Show("Sie müssen eine Welt zum Laden auswählen", "Nicht möglich!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				DialogResult = DialogResult.None;
			}
			else
			{
				//Get selected world string
				string strWorldFile = Convert.ToString(lbWorldsAvail.SelectedItem);
				m_oWorldEditor.LoadWorld(strWorldFile);
			}
		}

		//Click on new
		//@ KR / 2014.07.07
		private void btnNew_Click(object sender, EventArgs e)
		{
			string strWorldName = tbNewName.Text;

			if(strWorldName == "")
			{
				MessageBox.Show("Sie müssen der neuen Welt einen Namen geben", "Nicht möglich!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}
			else
			{
				m_oWorldEditor.NewWorld(strWorldName);
				DialogResult = DialogResult.OK;
			}
		}
	}
}
